﻿using DevExpress.XtraEditors;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Card.Make.Pro.Form
{
    public partial class Frm_Detail : DevExpress.XtraEditors.XtraForm
    {
        public string Id { set; get; }
        public Frm_Detail()
        {
            InitializeComponent();

        }


        /// <summary>
        /// 用户编号
        /// </summary>



        SqlHelper sqlHelper = new SqlHelper();
        private void LoadDate()
        {
            DataTable dt = new DataTable();
            dt = sqlHelper.GetQueryResult("Select * from Card_Table where C_ID = '" + Id + "'");
            if (dt.Rows.Count == 0)
            {
                return;
            }
            //照片
            var photo = dt.Rows[0]["Photo"];
            if (photo != null && photo != DBNull.Value)
            {
                var b = (byte[])photo;
                if (b.Length > 0)
                {
                    MemoryStream stream = new MemoryStream(b, true);
                    stream.Write(b, 0, b.Length);
                    this.imgUser.Image = new Bitmap(stream);
                    stream.Close();
                }
            }


            //二维码
            var barCode = dt.Rows[0]["TwoDimensionBarcode"];
            if (barCode != null && barCode != DBNull.Value)
            {
                var c = (byte[])barCode;
                if (c.Length > 0)
                {
                    MemoryStream stream1 = new MemoryStream(c, true);
                    stream1.Write(c, 0, c.Length);
                    this.imgTwo.Image = new Bitmap(stream1);
                    stream1.Close();
                }
            }

            //txt框
            this.txtName.Text = dt.Rows[0]["UserName"].ToString();
            if (dt.Rows[0]["UserSex"].ToString().Equals("0"))
            {
                this.txtSex.Text = "未知";
            }
            else if (dt.Rows[0]["UserSex"].ToString().Equals("1"))
            {
                this.txtSex.Text = "男";
            }
            else if (dt.Rows[0]["UserSex"].ToString().Equals("2"))
            {
                this.txtSex.Text = "女";
            }

            this.txtGroup.Text = dt.Rows[0]["CardGroup"].ToString();
            this.txtUnit.Text = dt.Rows[0]["CardUnit"].ToString();
            this.txtDepart.Text = dt.Rows[0]["CardDepart"].ToString();
            this.txtCareer.Text = dt.Rows[0]["UserCareer"].ToString();
            this.txtC_ID.Text = dt.Rows[0]["C_ID"].ToString();
            this.txtCertifNo.Text = dt.Rows[0]["UserCertifyCardNo"].ToString();
            this.txtTel.Text = dt.Rows[0]["ContactTel"].ToString();
            this.txtCarNo.Text = dt.Rows[0]["CarNo"].ToString();
            this.txtCardNo.Text = dt.Rows[0]["CardID"].ToString();
            this.textBox10.Text = dt.Rows[0]["UserBirthDay"].ToString();
            if (dt.Rows[0]["CardState"].ToString().Equals("0"))
            {
                this.txtCardStatus.Text = "正常";
            }
            else if (dt.Rows[0]["CardState"].ToString().Equals("1"))
            {
                this.txtCardStatus.Text = "已挂失";
            }
            else if (dt.Rows[0]["CardState"].ToString().Equals("2"))
            {
                this.txtCardStatus.Text = "已注销";
            }
            else if (dt.Rows[0]["CardState"].ToString().Equals("3"))
            {
                this.txtCardStatus.Text = "申请制证";
            }

            this.txtIFCertificate.Text = dt.Rows[0]["IfCertificate"].ToString().Equals("0") ? "未审批" : "已审批";
            this.txtIFPrint.Text = dt.Rows[0]["IFPrinted"].ToString().Equals("0") ? "未打印" : "已打印";
            this.txtIFGranted.Text = dt.Rows[0]["IFGranted"].ToString().Equals("0") ? "未发放" : "已发放";

            if (!dt.Rows[0]["UserBirthday"].ToString().Equals(""))
            {
                this.txtCreateDT.Text = ((DateTime)dt.Rows[0]["UserBirthday"]).ToString("yyyy-MM-dd");
            }
            if (!dt.Rows[0]["StartUseDT"].ToString().Equals(""))
            {
                this.txtCreateDT.Text = ((DateTime)dt.Rows[0]["StartUseDT"]).ToString("yyyy-MM-dd HH:mm:ss");
            }
            if (!dt.Rows[0]["ExpireDT"].ToString().Equals(""))
            {
                this.txtExpireDT.Text = ((DateTime)dt.Rows[0]["ExpireDT"]).ToString("yyyy-MM-dd HH:mm:ss");
            }
            if (!dt.Rows[0]["PrintDT"].ToString().Equals(""))
            {
                this.txtPrintDT.Text = ((DateTime)dt.Rows[0]["PrintDT"]).ToString("yyyy-MM-dd HH:mm:ss");
            }
            if (!dt.Rows[0]["GrantedDT"].ToString().Equals(""))
            {
                this.txtGrantedDT.Text = ((DateTime)dt.Rows[0]["GrantedDT"]).ToString("yyyy-MM-dd HH:mm:ss");
            }
            if (!dt.Rows[0]["ModifyDT"].ToString().Equals(""))
            {
                this.txtModiftDT.Text = ((DateTime)dt.Rows[0]["ModifyDT"]).ToString("yyyy-MM-dd HH:mm:ss");
            }
            if (!dt.Rows[0]["CreateDT"].ToString().Equals(""))
            {
                this.txtCreateDT.Text = ((DateTime)dt.Rows[0]["CreateDT"]).ToString("yyyy-MM-dd HH:mm:ss");
            }
            //姓名
            this.txtCardType.Text = dt.Rows[0]["CardType"].ToString();
            this.txtRatifierCID.Text = dt.Rows[0]["RatifierCID"].ToString();
            this.txtMemo.Text = dt.Rows[0]["CardMemo"].ToString();
            LoadUserDefine(dt);
        }
        private void LoadUserDefine(DataTable dt)
        {
            DataTable dt1 = sqlHelper.GetQueryResult("Select ParaStr From SysPara_Table where ParaIdent = 'UserDefines' and ParaIndex = '1'");
            if (dt1.Rows.Count == 0)
            {
                return;
            }
            string str = dt1.Rows[0]["ParaStr"].ToString();
            var item = str.Split('&');
            this.textBox38.Text = dt.Rows[0]["UserDefine1"].ToString();
            this.textBox31.Text = dt.Rows[0]["UserDefine2"].ToString();
            this.textBox37.Text = dt.Rows[0]["UserDefine3"].ToString();
            this.textBox32.Text = dt.Rows[0]["UserDefine4"].ToString();
            this.textBox41.Text = dt.Rows[0]["UserDefine5"].ToString();
            this.textBox35.Text = dt.Rows[0]["UserDefine6"].ToString();
            this.textBox39.Text = dt.Rows[0]["UserDefine7"].ToString();
            this.textBox33.Text = dt.Rows[0]["UserDefine8"].ToString();
            this.textBox40.Text = dt.Rows[0]["UserDefine9"].ToString();
            this.textBox34.Text = dt.Rows[0]["UserDefine10"].ToString();
            this.textBox42.Text = dt.Rows[0]["UserDefine11"].ToString();
            this.textBox36.Text = dt.Rows[0]["UserDefine12"].ToString();
            if (!item[0].ToString().Equals("UserDefine1"))
            {
                this.label31.Text = item[0].ToString() + ":";
            }
            if (!item[1].ToString().Equals("UserDefine2"))
            {
                this.label41.Text = item[1].ToString() + ":";
            }
            if (!item[2].ToString().Equals("UserDefine3"))
            {
                this.label35.Text = item[2].ToString() + ":";
            }
            if (!item[3].ToString().Equals("UserDefine4"))
            {
                this.label42.Text = item[3].ToString() + ":";
            }
            if (!item[4].ToString().Equals("UserDefine5"))
            {
                this.label36.Text = item[4].ToString() + ":";
            }
            if (!item[5].ToString().Equals("UserDefine6"))
            {
                this.label43.Text = item[5].ToString() + ":";
            }
            if (!item[6].ToString().Equals("UserDefine7"))
            {
                this.label37.Text = item[6].ToString() + ":";
            }
            if (!item[7].ToString().Equals("UserDefine8"))
            {
                this.label44.Text = item[7].ToString() + ":";
            }
            if (!item[8].ToString().Equals("UserDefine9"))
            {
                this.label39.Text = item[8].ToString() + ":";
            }
            if (!item[9].ToString().Equals("UserDefine10"))
            {
                this.label45.Text = item[9].ToString() + ":";
            }
            if (!item[10].ToString().Equals("UserDefine11"))
            {
                this.label40.Text = item[10].ToString() + ":";
            }
            if (!item[11].ToString().Equals("UserDefine12"))
            {
                this.label46.Text = item[11].ToString() + ":";
            }

        }

        private void LoadDetail()
        {
            DataTable dt = sqlHelper.GetQueryResult("select  EventDT,DomainName,VerID,EventType from HisList_Table where C_ID = '" + Id + "'");
            this.listView1.Items.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                this.listView1.Items.Add(new ListViewItem(new string[] { Convert.ToDateTime(dt.Rows[i]["EventDT"]).ToString("yyyy-MM-dd hh:mm:ss"), /*dt.Rows[i]["CardSnapShot"].Equals("") ? dt.Rows[i]["CardSnapShot"].ToString().Split(' ')[1]:"",*/ dt.Rows[i]["DomainName"].ToString(), dt.Rows[i]["VerID"].ToString(), /*dt.Rows[i]["EventType"].ToString()*/ }));
            }
        }

        private void FrmDetail_Shown(object sender, EventArgs e)
        {
            LoadDate();
            LoadDetail();
        }

        private void listViewEx1_MouseClick(object sender, MouseEventArgs e)
        {
            string VerID = this.listView1.SelectedItems[0].SubItems[2].Text.ToString();
            DataTable dt = sqlHelper.GetQueryResult("Select * from HisList_Table where VerID = '" + VerID + "'");
            if (dt.Rows.Count == 0)
            {
                return;
            }
            //上图
            var snapPic1 = dt.Rows[0]["SnapPic1"];
            if (snapPic1 != null && snapPic1 != DBNull.Value)
            {
                byte[] b = (byte[])snapPic1;
                if (b.Length > 0)
                {
                    MemoryStream stream = new MemoryStream(b, true);
                    stream.Write(b, 0, b.Length);
                    this.pictureBox3.Image = new Bitmap(stream);
                    stream.Close();
                }
            }

            #region 证卡类型
            string CardMode = "";

            if (dt.Rows[0]["CardMode"].ToString().Equals("0"))
            {
                CardMode = "车辆";
            }
            else if (dt.Rows[0]["CardMode"].ToString().Equals("1"))
            {
                CardMode = "人员";
            }
            else
            {
                CardMode = "物品";
            }
            #endregion

            #region 出入方向
            string EventType = "";
            if (dt.Rows[0]["EventType"].ToString().Equals("0"))
            {
                EventType = "未知";

            }
            else if (dt.Rows[0]["EventType"].ToString().Equals("1"))
            {
                EventType = "进";
            }
            else if (dt.Rows[0]["EventType"].ToString().Equals("2"))
            {
                EventType = "出";
            }
            #endregion

            #region 事件类型
            string eventType = "";
            if (dt.Rows[0]["EventType"].ToString().Equals("1"))
            {
                eventType = "正常通行";
            }
            else if (dt.Rows[0]["EventType"].ToString().Equals("2"))
            {
                eventType = "已挂失";
            }
            else if (dt.Rows[0]["EventType"].ToString().Equals("3"))
            {
                eventType = "已注销";

            }
            else if (dt.Rows[0]["EventType"].ToString().Equals("4"))
            {
                eventType = "未启用";
            }
            else if (dt.Rows[0]["EventType"].ToString().Equals("5"))
            {
                eventType = "已过期";
            }
            else if (dt.Rows[0]["EventType"].ToString().Equals("6"))
            {
                eventType = "无权同行";
            }
            else if (dt.Rows[0]["EventType"].ToString().Equals("7"))
            {
                eventType = "无此证件";
            }
            else if (dt.Rows[0]["EventType"].ToString().Equals("8"))
            {
                eventType = "无证通行";
            }
            else if (dt.Rows[0]["EventType"].ToString().Equals("9"))
            {
                eventType = "未发现属主证卡";
            }
            else if (dt.Rows[0]["EventType"].ToString().Equals("10"))
            {
                eventType = "按钮开门";
            }
            else if (dt.Rows[0]["EventType"].ToString().Equals("11"))
            {
                eventType = "远程开门";
            }
            else
            {
                eventType = "强行进入报警";
            }
            #endregion

            #region 姓名
            var list = (dt.Rows[0]["CardSnapShot"].ToString().Equals("") ? "" : dt.Rows[0]["CardSnapShot"].ToString()).Split(' ');
            string name = "";
            for (int i = 1; i < list.Length; i++)
            {
                name += list[i];
            }

            #endregion

            this.txtEvevtDT.Text = Convert.ToDateTime(dt.Rows[0]["EventDT"]).ToString("yyyy-MM-dd HH:mm:ss:fff");
            this.txtArea.Text = dt.Rows[0]["DomainName"].ToString();
            this.txtReader.Text = dt.Rows[0]["SrcDevice"].ToString();
            this.textBox2.Text = dt.Rows[0]["SrcData"].ToString();
            this.textBox7.Text = dt.Rows[0]["CardType"].ToString();
            this.textBox12.Text = CardMode;
            this.textBox3.Text = EventType;
            this.textBox8.Text = eventType;
            this.textBox1.Text = name;
            this.textBox5.Text = dt.Rows[0]["IPAddress"].ToString().Split(';')[0];
            if (dt.Rows[0]["FaceIdenRlt"].ToString().Equals("0"))
            {
                this.textBox4.Text = "";
            }
            else if (dt.Rows[0]["FaceIdenRlt"].ToString().Equals("1"))
            {
                this.textBox4.Text = "人证相符";
            }
            else if (dt.Rows[0]["FaceIdenRlt"].ToString().Equals("2"))
            {
                this.textBox4.Text = "人证不符";
            }
            else if (dt.Rows[0]["FaceIdenRlt"].ToString().Equals("3"))
            {
                this.textBox4.Text = "无有效人脸";
            }


        }

        private void Frm_Detail_Load(object sender, EventArgs e)
        {
            LoadDate();
            LoadDetail();
        }
    }
}